package com.szqhyxj.youxiangju.biz.plan;

import com.szqhyxj.youxiangju.biz.merchantHome.deviceinfo.DeviceInfo;
import com.szqhyxj.youxiangju.biz.merchantHome.deviceinfo.DeviceModel;
import com.szqhyxj.youxiangju.sys.BaseResult;

import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by leung on 2018/11/4.
 */

public class RepaymentPresenter implements RepaymentContract.Presenter {
    RepaymentContract.View view;

    public RepaymentPresenter(RepaymentContract.View view) {
        this.view = view;
    }
    public RepaymentPresenter() {

    }

    @Override
    public void onDestroy() {

    }


    @Override
    public void init() {
        RepaymentModel.getRepayment()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Consumer<BaseResult<List<RepaymentModel.RepaymentEntity>>>() {
                    @Override
                    public void accept(BaseResult<List<RepaymentModel.RepaymentEntity>> listBaseResult) throws Exception {
                        if (listBaseResult.businessLogicSuccess()) {
                            view.showList(listBaseResult.getData());
                        } else {
                            view.showToast(listBaseResult.getMessage());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        view.showToast(throwable.getMessage());
                    }
                });
    }

    @Override
    public void BilledCardFragment() {
        view.showBilledCardFragment();
    }

    @Override
    public void UnbilledCardFragment() {
        view.showUnbulledCardFragment();
    }

    @Override
    public void setdeviceinfo() {
        DeviceModel.getdeviceinfo()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Consumer<BaseResult<DeviceModel.DeviceInfoBean>>() {
                    @Override
                    public void accept(BaseResult<DeviceModel.DeviceInfoBean> deviceInfoBaseResult) throws Exception {
                        if (deviceInfoBaseResult.businessLogicSuccess()) {
                            DeviceModel.DeviceInfoBean deviceInfoBean = deviceInfoBaseResult.getData();
                            DeviceInfo.Holder.getInstance().setAreaInfo(deviceInfoBean.getAreaInfo());
                            DeviceInfo.Holder.getInstance().setDevNo(deviceInfoBean.getDevNo());
                            DeviceInfo.Holder.getInstance().setAepnType(deviceInfoBean.getAepnType());
                            DeviceInfo.Holder.getInstance().setLocalVersion(deviceInfoBean.getLocalVersion());
                            DeviceInfo.Holder.getInstance().setBattery(deviceInfoBean.getBattery());
                            DeviceInfo.Holder.getInstance().setUpdateDate(deviceInfoBean.getUpdateDate());
                            DeviceInfo.Holder.getInstance().setOnlineState(deviceInfoBean.getOnlineState());
                            view.showuserinfo();

                        } else {
                            if (deviceInfoBaseResult.getCode().equals("0001")){
                                view.enshowuserInfo();
                            }else{
                                view.showToast(deviceInfoBaseResult.getMessage() + "");
                            }
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        view.showToast(throwable.getMessage() + "");
                    }
                });
    }
}
